<template>
	<view>
		<view class="home">
			<view class="content">
				<view @click="goDetail(item._id)" class="item" v-for="item in listArr" :key="item._id">
					<view class="text">
						<view class="title">{{item.title}}</view>
						<view class="info">
							<text>{{item.author}}</text>

							<uni-dateformat :date="item.uploadtime" :threshold="[60000, 3600000]"
								format="yyyy/MM/dd hh:mm:ss"></uni-dateformat>
						</view>
					</view>

					<view class="pic">
						<image v-if="item.picurls && item.picurls.length" mode="aspectFill" :src="item.picurls[0]">
						</image>
						<image v-else mode="aspectFill" src="../../static/images/nopic.jpg"></image>
					</view>
				</view>
			</view>

			<view class="goAdd" @click="goAdd">
				<uni-icons type="plusempty" size="30" color="#fff"></uni-icons>
			</view>

			<view class="loading" v-if="!listArr.length">
				<uni-load-more status="loading"></uni-load-more>
			</view>

			<view class="loading" v-if="listArr.length">
				<uni-load-more :status="loadStatus"></uni-load-more>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				listArr: [],
				loadStatus: "more",
				pageSize: 6
			}
		},
		onLoad() {
			this.getData()
		},
		onReachBottom() {
			this.loadStatus = "loading"
			setTimeout(() => {
				this.getData()
			}, 500)
		},
		//下拉刷新
		onPullDownRefresh() {
			this.listArr = []
			this.getData();
		},
		methods: {
			//获取数据库的列表
			getData() {
				uniCloud.callFunction({
					name: "CloudArticle-get_all",
					data: {
						pages: this.listArr.length,
						pageSize: this.pageSize
					}
				}).then(res => {
					console.log(res);
					res.result.data.length == 0 ? this.loadStatus = "noMore" : this.loadStatus = "more"
					let oldList = this.listArr;
					let nsList = [...oldList, ...res.result.data]
					this.listArr = nsList
					uni.stopPullDownRefresh()
				})
			},
			//跳转到详情页
			goDetail(e) {
				uni.navigateTo({
					url: "/pages/detail/detail?id=" + e
				})
			},
			//点击跳转到新增页面
			goAdd() {
				uni.navigateTo({
					url: "/pages/add/add"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.home {
		.content {
			padding: 30rpx;

			.item {
				display: flex;
				justify-content: space-between;
				padding: 20rpx 0;
				border-bottom: 1rpx solid #eee;

				.text {
					flex: 1;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					padding-right: 20rpx;

					.title {
						font-size: 44rpx;
						color: #333;
						text-align: justify;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						line-clamp: 2;
						-webkit-box-orient: vertical;
					}

					.info {
						font-size: 28rpx;
						color: #888;

						text {
							padding-right: 20rpx;
						}
					}
				}

				.pic {
					width: 260rpx;
					height: 180rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.goAdd {
			width: 120rpx;
			height: 120rpx;
			background: #2B9939;
			color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 50%;
			font-size: 50rpx;
			position: fixed;
			right: 60rpx;
			bottom: 100rpx;
			box-shadow: 0 0 20rpx rgba(43, 153, 57, 0.7);
		}

		.loading {
			height: 230rpx;
			text-align: center;
			font-size: 26rpx;
			line-height: 2em;
			color: #888;
		}
	}
</style>